from django.http import HttpResponse
from django.shortcuts import render_to_response
from ManageMoney.models import Category, Transaction
from django.http import Http404
from StartGroup.models import Group
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django import forms
import datetime
import simplejson
import connectpeople.Constants
import Constants
from ManageMoney.models import CategoryForm, TransactionForm
from django.http import QueryDict
from django.conf import settings
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from utils.widgets import DatePicker

#############################################################
# function is called:
#   1) when web request for ManageMoney is sent
#   2) Ajax post request is sent to delete transactions
#
# returns
#   1) all transactions currently in DB
#   2) Unbound transaction form                             
#############################################################
def home(request):
    transaction_form = TransactionForm()
      
    #currently only supports deleting transaction(s)
    if request.POST:
        txn_ids_list = request.POST.getlist('checked_transaction_ids[]')

        # loop through each transaction to be deleted
        for txn_id in txn_ids_list:
            Transaction.objects.get(id=txn_id).delete()        
        
        # Set response to success
        response = simplejson.dumps({'success':'success'})
       
        if request.is_ajax():
            return HttpResponse(response, content_type="application/javascript")
        
    transactions = Transaction.objects.all()
    
    return render_to_response('ManageMoney/home.html', {'transactions': transactions, 'transaction_form' : transaction_form}, context_instance=RequestContext(request))

#############################################################
# function is called when client sends a request for adding a transaction
# returns
#   1) all transactions currently in DB
#   2) Unbound transaction form                             
#############################################################

def add_funding(request):
    if request.POST:
        transaction_form = TransactionForm(request.POST)
        
        if transaction_form.is_valid():
            if connectpeople.Constants.session_group_web_address in request.session:
                group_web_addr = request.session[connectpeople.Constants.session_group_web_address]
                userid = request.session[connectpeople.Constants.session_userid]
                group = getGroupInfo(group_web_addr)

            transaction_form.save(group)
        else:
            print transaction_form.errors
        
    return render_to_response( 'ManageMoney/home.html' , {'transactions': Transaction.objects.all(), 'transaction_form' : TransactionForm()}, context_instance = RequestContext(request)) 

# Helper function to obtain Group related to a group_web_address
def getGroupInfo(group_web_addr):
    return Group.objects.get(group_web_address= group_web_addr)      

        
        
